<?php
    if(!isset($_GET['pass']) || !isset($_GET['type']))
        die();
        
    if($_GET['pass'] != '123456')
        die();

	$con = mysql_connect('localhost','root','root');
	if(!$con)
	{
		die(mysql_error());
	}
    
	mysql_query('set names utf8');
	mysql_select_db('nexusget');
    
    $type = $_GET['type'];
    
    if($type == 'is_get_torrent')
    {
        if(!isset($_GET['ori_torrent_id']))
            die();
        $ori_torrent_id = 0 + $_GET['ori_torrent_id'];
        
        $sql = "SELECT id FROM torrent where ori_torrent_id = $ori_torrent_id";
        $result = mysql_query($sql);
        if(mysql_num_rows($result))
            echo '1';
        else
            echo '0';
    }
    elseif($type == 'get_torrent')
    {
        if(!isset($_GET['ori_torrent_id']))
            die();
        if(!isset($_GET['nexus_title']))
            die();
            
        $ori_torrent_id = 0 + $_GET['ori_torrent_id'];
        $nexus_title = mysql_real_escape_string($_GET['nexus_title']);
        
        $sql = "INSERT INTO `torrent` (`ori_torrent_id`,`title`) VALUES ($ori_torrent_id, '$nexus_title')";
        mysql_query($sql);
    }
    elseif($type == 'update_torrent_task_status')
    {
        if(!isset($_GET['ori_torrent_id']))
            die();
        if(!isset($_GET['torrent_status']))
            die();
            
        $torrent_status = 0 + $_GET['torrent_status'];
        $ori_torrent_id = 0 + $_GET['ori_torrent_id'];

        $sql = "UPDATE `torrent` SET status = $torrent_status where ori_torrent_id = $ori_torrent_id";
        mysql_query($sql);
    }
    elseif($type == 'get_torrent_id')
    {
        if(!isset($_GET['baidu_source_url']))
            die();

        $baidu_source_url = mysql_real_escape_string($_GET['baidu_source_url']);

        $sql = "SELECT ori_torrent_id FROM torrent where source_url = '$baidu_source_url'";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);
        if($row)
            echo $row['ori_torrent_id'];
        else
            echo '-1';
    }
    elseif($type == 'update_source_url')
    {
        if(!isset($_GET['ori_torrent_id']))
            die();
        if(!isset($_GET['baidu_source_url']))
            die();
            
        $ori_torrent_id = 0 + $_GET['ori_torrent_id'];
        $baidu_source_url = mysql_real_escape_string($_GET['baidu_source_url']);
        
        $sql = "UPDATE `torrent` SET source_url = '$baidu_source_url' where ori_torrent_id = $ori_torrent_id";
        mysql_query($sql);
    }
    elseif($type == 'update_info_hash')
    {
        if(!isset($_GET['ori_torrent_id']))
            die();
        if(!isset($_GET['torrent_info_hash']))
            die();
        
        $ori_torrent_id = 0 + $_GET['ori_torrent_id'];
        $torrent_info_hash = mysql_real_escape_string($_GET['torrent_info_hash']);

        $sql = "UPDATE `torrent` SET torrent_info_hash = '$torrent_info_hash' where ori_torrent_id = $ori_torrent_id";
        mysql_query($sql);
    }
    elseif($type == 'update_gift_code')
    {
        if(!isset($_GET['gift_code']))
            die();
        if(!isset($_GET['torrent_info_hash']))
            die();
        
        $gift_code = mysql_real_escape_string($_GET['gift_code']);
        $torrent_info_hash = mysql_real_escape_string($_GET['torrent_info_hash']);

        $sql = "UPDATE `torrent` SET gift_code = '$gift_code' where torrent_info_hash = '$torrent_info_hash'";
        mysql_query($sql);
        
        $sql = "SELECT gift_code FROM torrent where torrent_info_hash = '$torrent_info_hash'";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);
        if($row['gift_code'] == $gift_code)
            echo '0';
        else
            echo '1';
    }
	mysql_close();
?>